package is.ru.server;

import java.io.*;

/**
 * Created in IntelliJ IDEA.
 * Students: Jón Geir Sigurbjörnsson, Kristinn Helgi Hilmarsson, Jón Rúnar Arnarsson
 * Class: T-408-STNE, Stýrikerfi og netkerfi, 2011-1
 * Project: Programming assignment 3: HTTP web server
 * Date: 16.3.2011
 * Time: 20:17
 */

public class Request
{
    String line;

    public Request(String line)
    {
        this.line = line;
    }

    // Handle the Request and send a response
    public String getResponse()
    {
        String answer = "Bad Request!!";

        // split the request
        String requestLine[] = this.line.split("[+=?\\s+]");

        // Make sure that the request was not null or smaller then 2
        if (requestLine == null || requestLine.length < 2)
            return getHtml(answer);

        System.out.println("Requested URI: " + requestLine[1]);

        // Find out if the request is available else return index
        if (requestLine[1].equals("/addNumbers"))
        {
            // Make sure the length is longer then 3, to try to avoid errors if user put one number
            if (requestLine.length > 4)
            {
                answer = addNumbers(Integer.parseInt(requestLine[3]), Integer.parseInt(requestLine[4]));
            }
        }
        else if (requestLine[1].equals("/allCaps"))
        {
            if (requestLine.length > 2)
            {
                answer = allCaps(requestLine[3]);
            }
        }
        else if (requestLine[1].equals("/jvmStatus"))
        {
            answer = jvmStatus();
        }
        else
            answer = index();

        return getHtml(answer);
    }

    //Add the given numbers together and returns the answer
    public String addNumbers(int numberOne, int numberTwo)
    {
        int sum = numberOne + numberTwo;
        return ("The sum of " + numberOne + " and " + numberTwo + " is :" + sum);
    }

    // Return the line in UpperCase
    public String allCaps(String line)
    {
        return line.toUpperCase();
    }

    // Calculate the jvm
    public String jvmStatus()
    {
        Runtime HTTPServerRuntime = Runtime.getRuntime();
        long jvmAllocatedMemory = HTTPServerRuntime.totalMemory() / 1024;
        return ("The memory usage of the java virtual machine is: " + jvmAllocatedMemory);
    }


    // Return index from a file
    public String index()
    {
        String input = "";
        try
        {
            FileInputStream fis = new FileInputStream("index.html");
            BufferedReader bin = new BufferedReader(new InputStreamReader(fis));
            String line = "";

            while ((line = bin.readLine()) != null)
            {
                input += line;
            }
            fis.close();
            bin.close();

        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return input;
    }

    public String getHtml(String answer)
    {
        // Here we put the answer to the html
        return ("<html><head><title></title></head><body><p>" + answer + "</p><p><a href=\"/\"><strong>Back to index</strong></a></p></body></html>");

    }
}
